//
//  blePeripheral.h
//  MonitoringCenter
//
//  Created by David ding on 13-1-10.
//
//

#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
#import <UIKit/UIKit.h>

@protocol BLEPeripheralDelegate <NSObject>

-(void)didPeripheral:(CBPeripheral *)peripheral updateRSSI:(NSNumber *)RSSI;

@end

/**
 *  Peripheral的状态
 */
typedef NS_ENUM(NSInteger , BLEPeripheralDelegateState) {
    BLEPeripheralDelegateStateInit = 0,
    BLEPeripheralDelegateStateDiscoverServices,
    BLEPeripheralDelegateStateDiscoverCharacteristics,
    BLEPeripheralDelegateStateKeepActive,
};

/**
 *  BLEPeripheral 类主要封装了CBPeripheral（外围设备）并处理其回调方法。
 */
@interface BLEPeripheral : NSObject{
    BOOL enWrite;
    BOOL enNotify;
    NSTimeInterval lastTimeStamp;
}

@property (weak,nonatomic) id<BLEPeripheralDelegate> delegate;
//外部设备
@property(nonatomic,strong) CBPeripheral* activePeripheral;
//别名(用户重命名)，默认是peripheral.name
@property(strong,nonatomic) NSString* alias;
//信号
@property(strong,nonatomic) NSNumber* RSSI;
//CBService and CBCharacteristic
@property(readonly,nonatomic,strong) CBService* transDataService;
@property(readonly,nonatomic,strong) CBService* resetService;
@property(readonly,nonatomic,strong) CBCharacteristic* transDataCharateristic;
@property(readonly,nonatomic,strong) CBCharacteristic* resetCharateristic;
//状态
@property(readonly,nonatomic,assign) BLEPeripheralDelegateState currentPeripheralState;
//名称
@property(readonly,nonatomic,strong) NSString* nameString;
//uuid
@property(readonly,nonatomic,strong) NSString* uuidString;
@property(readonly,nonatomic,assign) BOOL connectedFinish;
@property(readonly,nonatomic,strong) NSData* receiveData;
@property(nonatomic,strong) NSData* sendData;
@property(nonatomic,assign) uint txCounter;
@property(nonatomic,assign) uint rxCounter;
@property(nonatomic,strong) NSString* showStringBuffer;
@property(nonatomic,strong) NSMutableArray* buffers;

/**
 *  初始化
 *
 *  @param peripheral 外设
 *
 *  @return id
 */
-(instancetype)initWithPeripheral:(CBPeripheral *)peripheral;


/**
 *  返回物理距离：根据RSSI计算得到
 *
 *  @return 手机和mBot之间的物理距离
 */
-(CGFloat)distanceByRSSI;

/**
 显示的名称

 @return  显示名称
 */
-(NSString *)displayName;

/**
 *  按UUID进行扫描
 *
 *  @param peripheral 外围设备
 */
-(void)startPeripheral:(CBPeripheral *)peripheral;

/**
 *  断开连接
 */
-(void)disconnectActivePeripheral;

/**
 *  发送数据(加入buffers队列)
 *
 *  @param data 数据
 */
-(void)sendData:(NSData *)data;

/**
 *  发送数据命令(直接发送，不加入buffers)
 *
 *  @param data 数据
 */
-(void)sendDataMandatory:(NSData *)data;

/**
 设置Airblock的GPIO

 @param gpio GPIO口 1、2、3、4
 @param isHighLevel 是否为高电平
 */
-(void)setAirblockGPIO:(NSInteger)gpio isHighLevel:(BOOL)isHighLevel;

/**
 设置是否跳过蓝牙校验位

 @param escape 跳过/不跳过
 */
-(void)setAirblockEscapeCheck:(BOOL)escape;

/**
 设置通用GPIO口的高低

 @param gpio GPIO口 1\2\3\4
 @param isHigh 是否为高电平
 */
- (void)setCommonGPIO:(NSInteger)gpio isHigh:(BOOL)isHigh;

/**
 *  读取数据
 */
-(void)readData;

/**
 *  重设IO
 */
-(void)resetIO;

@end
